/**
 * 
 */
package com.teamall.web.family.service.impl;

import java.math.*;
import java.util.*;

import javax.annotation.Resource;

import com.teamall.web.family.db.dao.FamilyHelpDao;
import com.teamall.web.family.db.po.FamilyHelpPO;
import com.teamall.web.sys.db.dao.SysUserDao;
import com.teamall.web.sys.db.dao.SysUserTokenDao;
import com.teamall.web.sys.db.po.SysUserPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teamall.core.common.Page;
import com.teamall.core.convert.IArgConversionService;
import com.teamall.core.idproduce.ISequenceGenerator;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.Utils;

import com.teamall.web.family.db.arg.FamilyReplyArg;
import com.teamall.web.family.db.arg.FamilyReplyArg.FamilyReplyCriteria;
import com.teamall.web.family.db.dao.FamilyReplyDao;
import com.teamall.web.family.db.po.FamilyReplyPO;
import com.teamall.web.family.service.IFamilyReplyService;

/**
 * <Description> <br>
 * 
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.family.service.impl <br>
 */

@Service("familyReplyService")
public class FamilyReplyServiceImpl implements IFamilyReplyService {

    private static final LogManager logger = LogManager
            .getLogger(FamilyReplyServiceImpl.class);

    @Autowired
    private FamilyHelpDao familyHelpDao;
    
    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private FamilyReplyDao familyReplyDao;
    /**
     * 查询条件转换成Arg类的服务接口
     */
    @Resource(name = "defaultArgConversionService")
    private IArgConversionService argConversionService;

    /**
     * 主键生成器
     */
    @Resource(name = "sequenceProcGenerator")
    private ISequenceGenerator sequenceGenerator;
    

    @Override
    public FamilyReplyPO selectByPrimaryKey(Integer key) throws BaseAppException {
        // ///////
        // TODO 根据业务场景，设置查询条件、数据校验等

        // ///////
        return familyReplyDao.selectByPrimaryKey(key);
    }

    @Override
    public List<FamilyReplyPO> selectByArg(FamilyReplyPO record) throws BaseAppException {
        logger.debug("selectByArg begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        FamilyReplyArg arg = new FamilyReplyArg();
        FamilyReplyCriteria criteria = arg.createCriteria();
        if(Utils.notEmpty(record.getHelpId())){
            criteria.andHelpIdEqualTo(record.getHelpId());
        }
        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改

        // ///////
        // TODO 根据业务场景，设置查询条件，示例
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }
        // ///////
        return familyReplyDao.selectByArg(arg);
    }

    @Override
    public Page<FamilyReplyPO> selectByArgAndPage(FamilyReplyPO record, Page<FamilyReplyPO> resultPage)
            throws BaseAppException {
        logger.debug("selectByArgAndPage begin...record={0}", record);

        FamilyReplyArg arg = new FamilyReplyArg();
        FamilyReplyCriteria criteria = arg.createCriteria();
        if(Utils.notEmpty(record.getHelpId())){
            criteria.andHelpIdEqualTo(record.getHelpId());
        }

        resultPage = familyReplyDao.selectByArgAndPage(arg, resultPage);


        return resultPage;
    }

    @Override
    public int add(FamilyReplyPO record) throws BaseAppException {
        logger.debug("add begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
        // int pkId = sequenceGenerator.sequenceIntValue("表名","主键名");
        // record.setUserId(pkId);
        // record.setCreatedDate(new Date());
        // ///////

        return familyReplyDao.insertSelective(record);
    }

    @Override
    public int update(FamilyReplyPO record) throws BaseAppException {
        logger.debug("update begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

        // ///////

        return familyReplyDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(FamilyReplyPO record) throws BaseAppException {
        logger.debug("delete begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行关联性校验、关联删除等

        // ///////

        return familyReplyDao.deleteByPrimaryKey(record.getReplyId());
    }

    @Override
    public int reply(Integer helpId,String content, Integer userId) throws BaseAppException {
        SysUserPO sysUserPO = sysUserDao.selectByPrimaryKey(userId);
        if(sysUserPO.getUserObjType() == 0){
            FamilyHelpPO po = familyHelpDao.selectByPrimaryKey(helpId);
            if(Utils.isEmpty(po)){
                return -1;
            }
            po.setFeedbackStaff(userId);
            po.setHelpState(0);
            po.setFeedbackDate(new Date());
            po.setFeedbackContent(content);
            familyHelpDao.updateByPrimaryKeySelective(po);
        }
        FamilyReplyPO familyReplyPO = new FamilyReplyPO();
        familyReplyPO.setCreateDate(new Date());
        familyReplyPO.setContent(content);
        familyReplyPO.setHelpId(helpId);
        familyReplyPO.setUserId(userId);
        familyReplyDao.insert(familyReplyPO);
        return 0;
    }

}
